package com.example.testscrcap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class MirrorSurface extends Activity {
	private final static String tag="mrsf";
	private SurfaceView msvFrame;
	private SurfaceHolder mHolder;
	private Surface mSurface;
	private String remoteIp;
	private int remotePort;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.d(tag, "OnCreate.......MirrorSurface1111110000");
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);	
		setContentView(R.layout.mrsurface);
		
		
		Intent intent = getIntent();
		remoteIp = intent.getExtras().getString("ip");
		remotePort = intent.getExtras().getInt("port");
		Log.d(tag, String.format("remote ip=%s,  port=%d", remoteIp, remotePort));
		
		
		
		msvFrame = (SurfaceView)findViewById(R.id.svFrame);
		mHolder = msvFrame.getHolder();
		//mHolder.addCallback(this);
		mHolder.addCallback(new SurfaceHolder.Callback() {
			
			@Override
			public void surfaceDestroyed(SurfaceHolder arg0) {
				// TODO Auto-generated method stub
				Log.d(tag, "mirror surface destroyed....");
				mSurface = null;
			}
			
			@Override
			public void surfaceCreated(SurfaceHolder arg0) {
				// TODO Auto-generated method stub
				Log.d(tag, "frame surface created....");
				arg0.setFormat(PixelFormat.RGBX_8888);
				//arg0.setFixedSize(720, 1280);
				//MainActivity.screenCap.nvdraw(mHolder.getSurface());
				if(mSurface == null) {
					mSurface = mHolder.getSurface();
					startRecv();
					
				}	
			}
			
			@Override
			public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		stopRecv();
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d(tag, "OnResume...");
		
	}
	
	
	public void startRecv() {
		MainActivity.screenCap.nvOpenMirror(mSurface, 1);
		MainActivity.screenCap.nvSetMirrorCfg(MainActivity.MirrorWidth, MainActivity.MirrorHeight);
		MainActivity.screenCap.nvStartRecvFrame(remoteIp, remotePort, MainActivity.Transport);
	}
	
	public void stopRecv() {
		MainActivity.screenCap.nvStopRecvFrame();
	}
	
	
	/*
	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		if(arg0 == mHolder) {
			Log.d(tag, "surface created....");
			arg0.setFormat(PixelFormat.RGBX_8888);
			MainActivity.screenCap.nvdraw(mHolder.getSurface());
		}	
		
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		
	}
	*/
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.d(tag, "OnStop....");
		mSurface = null;
		mHolder = null;
		msvFrame = null;
	}
}
